var app = getApp();
var util = require("../../utils/libs.js")
var api = require("../../utils/api.js")
Page({
  data: {
    image: '',
    title: '',
    content:"",
    uid:"",
    imgUrl:"",
    id:null,
    is_draft:1
  },
  onLoad (options) {
    let that = this
    var id=wx.getStorageSync("uid")
    that.setData({
      uid:id,
      id:options.id
    })
    // 页面初始化 options为页面跳转所带来的参数
  },
  getValue(event){
    let that = this
    let values = event.detail.value;
    that.setData({
      title: values
    })
   },
   closeImages(){
    this.setData({
      imgUrl:''
    })
  },
  chooseImageTap (){
    let _this = this;
    wx.showActionSheet({
     itemList: ['从相册中选择', '拍照'],
     itemColor: "#f7982a",
     success: function(res) {
     if (!res.cancel) {
     if(res.tapIndex == 0){
     _this.chooseWxImage('album')
     }else if(res.tapIndex == 1){
     _this.chooseWxImage('camera')
     }
     }
     },
     fail: function(res) {
      console.log(res.errMsg)
    }
    })
},
chooseWxImage:function(type){
  let _this = this;
  wx.chooseImage({
   sizeType: ['original', 'compressed'],
   sourceType: [type],
   success: function (res) {
   _this.setData({
   logo: res.tempFilePaths[0],
   })
   app.upload_file("https://bucuo.jikeyun.net/api/upload",
    _this.data.logo,"upload",{},function success(res){
        res = JSON.parse(res)
      if(res.error_code === 0){
        _this.setData({
          imgUrl:res.data.file
        })
      }
    })
 }
})
},
  bindTextAreaBlur(e){
    let content=e.detail.value
    let that = this
    that.setData({
      content:content
    })
  },
  addmend(){
    let that = this
    if(that.data.title==''){
      wx.showToast({
        title:'请输入标题',
        icon:"none",
        duration:1000
      })
      return false;
    }
    if(that.data.content==''){
      wx.showToast({
        title: '请输入修改内容',
        icon:"none",
        duration:1000
      })
      return false
    }
    util.request(api.articleAdd,{
      uid:that.data.uid,
      title:that.data.title,
      content:that.data.content,
      image:that.data.imgUrl
    },"POST")
    .then(function (res) {
      if(res.error_code === 0){
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          success:function(){
            let id = res.data.id
            that.setData({
              id:id
            })
            wx.navigateTo({
              url: '/pages/assist/assist?id='+id
            })
          }
        })
      }
    })
  },
  addSave(){
    let that = this
    if(that.data.title==''){
      wx.showToast({
        title: '请输入文章标题',
        icon:"none",
        duration:1000
      })
      return false
    }
    if(that.data.content==""){
      wx.showToast({
        title: '请输入文章内容',
        icon:"none",
        duration:1000
      })
      return false
    }
    util.request(api.articleAdd,{
      uid:that.data.uid,
      title:that.data.title,
      content:that.data.content,
      image:that.data.imgUrl,
      is_draft:that.data.is_draft ,
      id:that.data.id
    },"POST")
    .then(function (res) {
      if(res.error_code === 0){
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000,
          success:function(){
            let id = res.data.id
            that.setData({
              id:id
            })
            wx.navigateTo({
              url: '/pages/works/works'
            })   
          }
        })
      }
    })
   
  },
  onReady () {
    // 页面渲染完成
  },
  onShow () {
    // 页面显示
  },
  onHide () {
    // 页面隐藏

  },
  onUnload () {
    // 页面关闭

  }
})
